﻿using Pay.Core;
using Pay.DTO.card;
using Pay.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pay.Api.Controllers
{
    [RoutePrefix("card")]
    public class CardController : Controller
    {
        private ICardService _iCardService;

        public CardController(ICardService iCardService) {
            _iCardService = iCardService;
        }

        [Route("notify")]
        public ActionResult XsftQuickNotify(card_notify notify) {
            var log = IocHelper.Get<ILogHelper>("info");
            log.Info("信用卡申请异步通知了:" + notify.clientNo);

            if (notify.callbackType == "CALLBACK_SUCCESS") {
                log.Info("信用卡异步通知成功:" + notify.clientNo);
                var result = 0;
                var card = _iCardService.GetByOrderNo(notify.clientNo);
                if (card != null) {
                    if (card.status == 1) {
                        result = _iCardService.CardSuccess(new card_update { clientNo = notify.clientNo, status = 2 });
                        if (result > 0) {
                            return Json(new { callbackType = "CALLBACK_SUCCESS", message = "信用卡申请成功" }, JsonRequestBehavior.AllowGet);
                        }
                    } else {
                        if(card.status == 2)
                            return Json(new { callbackType = "CALLBACK_FAIL", message = "信用卡已申请成功" }, JsonRequestBehavior.AllowGet);
                        if(card.status == 3)
                            return Json(new { callbackType = "CALLBACK_FAIL", message = "信用卡已取消申请" }, JsonRequestBehavior.AllowGet);
                    }
                } else {
                    return Json(new { callbackType = "CALLBACK_FAIL", message = "请求失败" }, JsonRequestBehavior.AllowGet);
                }
            }
            return Json(new { callbackType = "CALLBACK_FAIL", message = "请求失败" }, JsonRequestBehavior.AllowGet);
        }
    }
}